package com.management.department.teacherinfosystem.Repository;

import com.management.department.teacherinfosystem.Entity.ResearchProject;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface ResearchProjectRepository extends JpaRepository<ResearchProject, Long>, JpaSpecificationExecutor<ResearchProject> {
    
    // 根据负责人ID查询项目
    List<ResearchProject> findByLeaderId(Long leaderId);
    
    // 根据状态查询项目
    List<ResearchProject> findByStatus(String status);
    
    // 根据多个状态查询项目
    List<ResearchProject> findByStatusIn(List<String> statuses);
    
    // 根据项目级别查询
    List<ResearchProject> findByLevel(String level);
    
    // 根据项目类型查询
    List<ResearchProject> findByProjectType(String projectType);
    
    // 根据学科分类查询
    List<ResearchProject> findBySubjectCategory(String subjectCategory);
    
    // 模糊查询项目名称
    List<ResearchProject> findByNameContaining(String name);
    
    // 根据项目编号查询
    ResearchProject findByProjectNumber(String projectNumber);
    
    // 查询待审核的项目（申报中和结题审核中）
    @Query("SELECT p FROM ResearchProject p WHERE p.status IN ('申报中', '结题审核中') ORDER BY p.createdAt DESC")
    List<ResearchProject> findPendingReviewProjects();
    
    // 根据来源单位查询
    List<ResearchProject> findBySourceUnit(String sourceUnit);
    
    // 查询某个用户参与的所有项目（作为负责人或成员）
    @Query("SELECT DISTINCT p FROM ResearchProject p LEFT JOIN ResearchProjectMember m ON p.id = m.projectId " +
           "WHERE p.leaderId = :userId OR m.userId = :userId ORDER BY p.updatedAt DESC")
    List<ResearchProject> findProjectsByUserId(@Param("userId") Long userId);
}
